unit uFrmConfiguracoes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, JvExComCtrls, JvComCtrls, FMTBcd, DB, DBClient,
  Provider, SqlExpr, StdCtrls, Grids, DBGrids, Mask, DBCtrls, SynEdit, SynDBEdit,
  SynEditHighlighter, SynHighlighterSQL;

type
  TFrmConfiguracoes = class(TForm)
    ts: TPageControl;
    tsConfigTabelaPesquisa: TTabSheet;
    Panel1: TPanel;
    Panel2: TPanel;
    DBGrid1: TDBGrid;
    dsConfigTabelaPesquisa: TDataSource;
    sdsConfigTabelaPesquisa: TSQLDataSet;
    dspConfigTabelaPesquisa: TDataSetProvider;
    cdsConfigTabelaPesquisa: TClientDataSet;
    cdsTempTipoCampo: TClientDataSet;
    cdsTempTipoCampoTIPO: TStringField;
    cdsTempTipoCampoDESCRICAO: TStringField;
    Label3: TLabel;
    DBLCBTipoCampo: TDBLookupComboBox;
    Label4: TLabel;
    DBETamanhoCampo: TDBEdit;
    Label5: TLabel;
    DBELabelCampo: TDBEdit;
    Label6: TLabel;
    DBEApelidoTabela: TDBEdit;
    Label7: TLabel;
    DBLCLExibeNoDbGrid: TDBLookupComboBox;
    DBLCBExibeNoComboBox: TDBLookupComboBox;
    Label9: TLabel;
    DBELarguraColunaDBGrid: TDBEdit;
    LABEL8: TLabel;
    dsTempTipoCampo: TDataSource;
    dsSimNao: TDataSource;
    PNLBotoes: TPanel;
    BtnInserir: TButton;
    BtnSalvar: TButton;
    BtnCancelar: TButton;
    BtnExcluir: TButton;
    BtnSubirPosicao: TButton;
    BtnDescerPosicao: TButton;
    BtnGerarNumeracao: TButton;
    sdsConfigTabelaPesquisaTABELA_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaCAMPO_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaTIPO_CAMPO_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaTAMANHO_CAMPO_PESQUISA: TIntegerField;
    sdsConfigTabelaPesquisaLABEL_CAMPO_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaAPELIDO_TABELA_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaDBGRID_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaCOMBOBOX_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaLARGURA_COLUNA_PESQUISA: TIntegerField;
    sdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA: TIntegerField;
    sdsConfigTabelaPesquisaTIPO_REL_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaTABELA2_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaCAMPO2_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaAPELIDO_TABELA2_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaCAMPOCHAVE_TABELA1_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaCAMPOCHAVE_TABELA2_PESQUISA: TStringField;
    sdsConfigTabelaPesquisaUSA_RELACIONAMENTO_TABELA: TStringField;
    cdsConfigTabelaPesquisaTABELA_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaCAMPO_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaTIPO_CAMPO_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaTAMANHO_CAMPO_PESQUISA: TIntegerField;
    cdsConfigTabelaPesquisaLABEL_CAMPO_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaAPELIDO_TABELA_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaDBGRID_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaCOMBOBOX_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaLARGURA_COLUNA_PESQUISA: TIntegerField;
    cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA: TIntegerField;
    cdsConfigTabelaPesquisaTIPO_REL_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaTABELA2_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaCAMPO2_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaAPELIDO_TABELA2_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaCAMPOCHAVE_TABELA1_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaCAMPOCHAVE_TABELA2_PESQUISA: TStringField;
    cdsConfigTabelaPesquisaUSA_RELACIONAMENTO_TABELA: TStringField;
    Label1: TLabel;
    cbTabela: TComboBox;
    Label2: TLabel;
    cdsCampos: TClientDataSet;
    dsCampos: TDataSource;
    cdsCamposnomeCampo: TStringField;
    DBLCBCampo: TDBLookupComboBox;
    cdsTabelas: TClientDataSet;
    cdsCampos2: TClientDataSet;
    cdsTabelastabela: TStringField;
    dsTabelas: TDataSource;
    dsCampos2: TDataSource;
    cdsCampos2nomeCampo: TStringField;
    DBLCBUsaRelacionamento: TDBLookupComboBox;
    Label13: TLabel;
    cdsTipoRel: TClientDataSet;
    cdsTipoReltiporRel: TStringField;
    PNLRelacionamento: TPanel;
    GroupBox1: TGroupBox;
    Label10: TLabel;
    DBLCBTabela2: TDBLookupComboBox;
    Label11: TLabel;
    DBEApelidoTabela2: TDBEdit;
    Label12: TLabel;
    DBLCBCampo2: TDBLookupComboBox;
    DBLCBTipoRelacionamento: TDBLookupComboBox;
    Label14: TLabel;
    DBLCBCampoChaveTabela1: TDBLookupComboBox;
    DBLCBCampoChaveTabela2: TDBLookupComboBox;
    Label15: TLabel;
    Label16: TLabel;
    dsTipoRel: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure cbTabelaChange(Sender: TObject);
    procedure BtnInserirClick(Sender: TObject);
    procedure BtnSalvarClick(Sender: TObject);
    procedure HabilitaComponentesConfigTabelaPesquisa;
    procedure DesabilitaComponentesConfigTabelaPesquisa;
    procedure BtnCancelarClick(Sender: TObject);
    procedure BtnExcluirClick(Sender: TObject);
    procedure GerarNumeracao;
    function VerificaArrayNumS (vArray : array of Integer ; vNum : integer) : Boolean;
    function GeraNumeroValido (vArray : array of Integer) : Integer;
    procedure DescerPosicao;
    procedure SubirPosicao;
    procedure BtnGerarNumeracaoClick(Sender: TObject);
    procedure BtnDescerPosicaoClick(Sender: TObject);
    procedure BtnSubirPosicaoClick(Sender: TObject);
    procedure dsConfigTabelaPesquisaDataChange(Sender: TObject; Field: TField);
    procedure cdsConfigTabelaPesquisaAfterPost(DataSet: TDataSet);
    procedure cdsConfigTabelaPesquisaAfterDelete(DataSet: TDataSet);
    procedure PopularCdsCampos;
    procedure PopularCdsTabela;
    procedure PopularCdsCampos2;
    procedure PopularCdsTipoRel;
    procedure DBGrid1Enter(Sender: TObject);
    function ObterApelidoTabela(NomeTabela : string) : string;
    procedure DBLCBTabela2Exit(Sender: TObject);
    procedure DBEApelidoTabelaEnter(Sender: TObject);
  private
    NomeTabela2 : string;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmConfiguracoes: TFrmConfiguracoes;

implementation

uses uDM1, uClasses;

{$R *.dfm}

procedure TFrmConfiguracoes.BtnCancelarClick(Sender: TObject);
begin
  cdsConfigTabelaPesquisa.Cancel;
end;

procedure TFrmConfiguracoes.BtnDescerPosicaoClick(Sender: TObject);
begin
  SubirPosicao; //DescerPosicao;
end;

procedure TFrmConfiguracoes.BtnExcluirClick(Sender: TObject);
begin
  cdsConfigTabelaPesquisa.Delete;
end;

procedure TFrmConfiguracoes.BtnGerarNumeracaoClick(Sender: TObject);
begin
  GerarNumeracao;
end;

procedure TFrmConfiguracoes.BtnInserirClick(Sender: TObject);
begin
  cdsConfigTabelaPesquisa.Insert;
  cdsConfigTabelaPesquisaTABELA_PESQUISA.Value := cbTabela.Text;
  //cdsConfigTabelaPesquisaCAMPO_PESQUISA.Value := cbCampo.Text;
end;

procedure TFrmConfiguracoes.BtnSalvarClick(Sender: TObject);
begin
  cdsConfigTabelaPesquisa.Post;
end;

procedure TFrmConfiguracoes.BtnSubirPosicaoClick(Sender: TObject);
begin
  DescerPosicao;  //SubirPosicao;
end;

procedure TFrmConfiguracoes.cbTabelaChange(Sender: TObject);
begin
  if cbTabela.Text <> '' then
  begin
    //cbCampo.Items.Clear;
    //DM1.CONEXAO.GetFieldNames(cbTabela.Text,cbCampo.Items);
    //cdsConfigTabelaPesquisa.Close;
    PopularCdsCampos;
    sdsConfigTabelaPesquisa.Close;
    sdsConfigTabelaPesquisa.CommandText := 'select * from campo_pesquisa where tabela_pesquisa = ' + QuotedStr(cbTabela.Text) + ' order by ordem_campo_pesquisa asc';
    sdsConfigTabelaPesquisa.Open;
    cdsConfigTabelaPesquisa.Refresh;
    HabilitaComponentesConfigTabelaPesquisa;
  end;

end;

procedure TFrmConfiguracoes.cdsConfigTabelaPesquisaAfterDelete(
  DataSet: TDataSet);
begin
  cdsConfigTabelaPesquisa.ApplyUpdates(0);
end;

procedure TFrmConfiguracoes.cdsConfigTabelaPesquisaAfterPost(DataSet: TDataSet);
begin
  cdsConfigTabelaPesquisa.ApplyUpdates(0);
end;

procedure TFrmConfiguracoes.DBEApelidoTabelaEnter(Sender: TObject);
begin
  if dsConfigTabelaPesquisa.State = dsInsert then
    cdsConfigTabelaPesquisaAPELIDO_TABELA_PESQUISA.Value := ObterApelidoTabela(cbTabela.Text);

end;

procedure TFrmConfiguracoes.DBGrid1Enter(Sender: TObject);
begin
  Click;
end;

procedure TFrmConfiguracoes.DBLCBTabela2Exit(Sender: TObject);
begin
  if dsConfigTabelaPesquisa.State = dsInsert then
    cdsConfigTabelaPesquisaAPELIDO_TABELA2_PESQUISA.Value := ObterApelidoTabela(cdsConfigTabelaPesquisaTABELA2_PESQUISA.Value);

end;

procedure TFrmConfiguracoes.DesabilitaComponentesConfigTabelaPesquisa;
begin
{  BtnInserir.Enabled := false;
  BtnSalvar.Enabled := False;
  BtnCancelar.Enabled := False;
  BtnExcluir.Enabled := False;}
  PNLBotoes.Enabled := False;
  DBLCBTipoCampo.Enabled := False;
  DBETamanhoCampo.Enabled := False;
  DBELabelCampo.Enabled := False;
  DBEApelidoTabela.Enabled := False;
  DBLCLExibeNoDbGrid.Enabled := False;
  DBLCBExibeNoComboBox.Enabled := False;
  DBELarguraColunaDBGrid.Enabled := False;
  BtnSubirPosicao.Enabled := False;
  BtnDescerPosicao.Enabled := false;
  BtnGerarNumeracao.Enabled := False;
  DBGrid1.Enabled := False;
  DBLCBCampo.Enabled := False;
//  DBCHBUsaRelacionamento.Enabled := False;
end;

procedure TFrmConfiguracoes.DescerPosicao;
var NumAnterio , NumProximo , NumAtual : Integer;
 Book : TBookmark;
begin
  if (not cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.IsNull) and
  ( cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value <> 1)  then
  begin
    Book := cdsConfigTabelaPesquisa.GetBookmark;
    NumAtual := cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value;
    if cdsConfigTabelaPesquisa.Locate('ORDEM_CAMPO_PESQUISA',(NumAtual - 1),[]) then
    begin
      cdsConfigTabelaPesquisa.Edit;
      cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value := NumAtual;
      cdsConfigTabelaPesquisa.Post;
      cdsConfigTabelaPesquisa.GotoBookmark(Book);
      cdsConfigTabelaPesquisa.Edit;
      cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value := NumAtual - 1;
      cdsConfigTabelaPesquisa.Post;
    end
    else
    begin
      cdsConfigTabelaPesquisa.Edit;
      cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value := NumAtual - 1;
      cdsConfigTabelaPesquisa.Post;
    end;


  end;


end;

procedure TFrmConfiguracoes.dsConfigTabelaPesquisaDataChange(Sender: TObject;
  Field: TField);
begin
  if (cbTabela.Text <> '') then
  begin
    BtnInserir.Enabled := (dsConfigTabelaPesquisa.State in [dsBrowse]);
    BtnSalvar.Enabled := (dsConfigTabelaPesquisa.state in [dsEdit,dsInsert]);
    BtnCancelar.Enabled := (dsConfigTabelaPesquisa.state in [dsEdit,dsInsert]);
    BtnExcluir.Enabled := (dsConfigTabelaPesquisa.state in [dsBrowse]);
    BtnSubirPosicao.Enabled := (dsConfigTabelaPesquisa.state in [dsBrowse]);
    BtnDescerPosicao.Enabled := (dsConfigTabelaPesquisa.state in [dsBrowse]);
    BtnGerarNumeracao.Enabled := (dsConfigTabelaPesquisa.state in [dsBrowse]);
    if cdsConfigTabelaPesquisaUSA_RELACIONAMENTO_TABELA.Value = 'T' then
    begin
      PNLRelacionamento.Visible := True;
      //Panel1.Height := 177;
    end
    else
    begin
      PNLRelacionamento.Visible := False;
      //Panel1.Height := 89;
    end;
  end;

  if (not cdsConfigTabelaPesquisaTABELA2_PESQUISA.IsNull) then
    PopularCdsCampos2;


end;

procedure TFrmConfiguracoes.FormCreate(Sender: TObject);
begin
  //Panel1.Height := 89;
  DM1.CONEXAO.GetTableNames(cbTabela.Items,false);
  //DM1.CONEXAO.GetTableNames(CBTabela2.Items,false);
  with cdsTempTipoCampo do
  begin
    Insert;
    cdsTempTipoCampoTIPO.Value := 'STRING';
    cdsTempTipoCampoDESCRICAO.Value := 'STRING';
    Post;
    Insert;
    cdsTempTipoCampoTIPO.Value := 'BOOLEAN';
    cdsTempTipoCampoDESCRICAO.Value := 'BOOLEAN';
    Post;
    Insert;
    cdsTempTipoCampoTIPO.Value := 'INTEGER';
    cdsTempTipoCampoDESCRICAO.Value := 'INTEGER';
    Post;
  end;
  TFuncoes.AbrirTodosSdsECds(Self);
  PopularCdsTabela;
  PopularCdsTipoRel;
  DesabilitaComponentesConfigTabelaPesquisa;
end;

function TFrmConfiguracoes.GeraNumeroValido(vArray: array of Integer): Integer;
var NumS , i , TamArray : Integer;
begin
  NumS := 1;
  TamArray := Length(vArray);
  Result := 0;
  for i := 0 to TamArray do
  begin
    if NumS <> vArray[i] then
    begin
      Result := NumS;
      Exit;
    end;
  end;


end;

procedure TFrmConfiguracoes.GerarNumeracao;
var Num , i , MaxNum , MinNum : Integer;
  NumS : array of Integer;
begin
  cdsConfigTabelaPesquisa.First;
  MaxNum := 1;
  MinNum := 1;
  Num := 1;
  //NumS := 1;
  SetLength(NumS,cdsConfigTabelaPesquisa.RecordCount);
  for i := 0 to cdsConfigTabelaPesquisa.RecordCount do
  begin
    if not cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.IsNull then NumS[i] := cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value;
    cdsConfigTabelaPesquisa.Next;
  end;

  cdsConfigTabelaPesquisa.First;
  for i := 0 to cdsConfigTabelaPesquisa.RecordCount do
  begin
    if cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value = 0 then
    begin
      //ShowMessage(cdsConfigTabelaPesquisaCAMPO_PESQUISA.Value);
      while cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value = 0 do
      begin
        cdsConfigTabelaPesquisa.Edit;
        if not VerificaArrayNumS(NumS,Num) then
        begin
          cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value := Num;
          cdsConfigTabelaPesquisa.Post;
        end;
        Num := Num + 1;
      end;
    end;
    cdsConfigTabelaPesquisa.First;
  end;


end;

procedure TFrmConfiguracoes.HabilitaComponentesConfigTabelaPesquisa;
begin
{  BtnInserir.Enabled := true;
  BtnSalvar.Enabled := true;
  BtnCancelar.Enabled := true;
  BtnExcluir.Enabled := true;}
  PNLBotoes.Enabled := True;
  DBLCBTipoCampo.Enabled := true;
  DBETamanhoCampo.Enabled := true;
  DBELabelCampo.Enabled := true;
  DBEApelidoTabela.Enabled := True;
  DBLCLExibeNoDbGrid.Enabled := True;
  DBLCBExibeNoComboBox.Enabled := True;
  DBELarguraColunaDBGrid.Enabled := True;
  BtnSubirPosicao.Enabled := True;
  BtnDescerPosicao.Enabled := True;
  BtnGerarNumeracao.Enabled := True;
  DBGrid1.Enabled := True;
  DBLCBCampo.Enabled := True;
//  DBCHBUsaRelacionamento.Enabled := True;
end;





function TFrmConfiguracoes.ObterApelidoTabela(NomeTabela: string): string;
var qryAux : TSQLQuery;
begin
  qryAux := nil;
  Result := '';
  try
    qryAux := TSQLQuery.Create(nil);
    qryAux.SQLConnection := DM1.CONEXAO;
    qryAux.SQL.Text := 'select first 1 APELIDO_TABELA_PESQUISA from CAMPO_PESQUISA ' +
    ' where TABELA_PESQUISA = ' + QuotedStr(NomeTabela) + ' and APELIDO_TABELA_PESQUISA is not null ';
    qryAux.Open;
    if not qryAux.IsEmpty then
      Result :=  qryAux.FieldByName('APELIDO_TABELA_PESQUISA').AsString;
  finally
    qryAux.Free;
  end;
end;

procedure TFrmConfiguracoes.PopularCdsCampos;
var listaAux : TStringList;
  i : Integer;
begin
  listaAux := nil;
  try
    listaAux := TStringList.Create;
    DM1.CONEXAO.GetFieldNames(cbTabela.Text,listaAux);
    cdsCampos.Close;
    cdsCampos.CreateDataSet;
    for i := 0 to listaAux.Count - 1 do
    begin
      cdsCampos.Insert;
      cdsCamposnomeCampo.Value := listaAux.Strings[i];
      cdsCampos.Post;
    end;
  finally
    listaAux.Free;
  end;
end;

procedure TFrmConfiguracoes.PopularCdsCampos2;
var listaCampos : TStringList;
i : Integer;
begin
  listaCampos := nil;
  try
    if cdsConfigTabelaPesquisaTABELA2_PESQUISA.Value <> NomeTabela2  then
    begin
      cdsCampos2.Close;
    cdsCampos2.CreateDataSet;
    listaCampos := TStringList.Create;
    DM1.CONEXAO.GetFieldNames(cdsConfigTabelaPesquisaTABELA2_PESQUISA.Value,listaCampos);
    for i := 0 to listaCampos.Count - 1 do
    begin
      cdsCampos2.Insert;
      cdsCampos2nomeCampo.Value := listaCampos[i];
      cdsCampos2.Post;
    end;
    end;
    NomeTabela2 := cdsConfigTabelaPesquisaTABELA2_PESQUISA.Value;
  finally
    listaCampos.Free;
  end;
end;

procedure TFrmConfiguracoes.PopularCdsTabela;
var listaTabelas : TStringList;
  i : Integer;
begin
  listaTabelas := nil;
  try
    listaTabelas := TStringList.Create;
    DM1.CONEXAO.GetTableNames(listaTabelas,false);
    for i := 0 to listaTabelas.Count - 1 do
    begin
      cdsTabelas.Insert;
      cdsTabelastabela.Value := listaTabelas[i];
      cdsTabelas.Post;
    end;
  finally
    listaTabelas.Free;
  end;
end;

procedure TFrmConfiguracoes.PopularCdsTipoRel;
begin
  with cdsTipoRel do
  begin
    Insert;
    cdsTipoReltiporRel.Value := 'INNER';
    Insert;
    cdsTipoReltiporRel.Value := 'LEFT';
    Insert;
    cdsTipoReltiporRel.Value := 'RIGHT';
    Post;
  end;
end;

procedure TFrmConfiguracoes.SubirPosicao;
var NumAnterio , NumProximo , NumAtual : Integer;
 Book : TBookmark;
begin
  if (not cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.IsNull) and
  ( cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value <> 1)  then
  begin
    Book := cdsConfigTabelaPesquisa.GetBookmark;
    NumAtual := cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value;
    if cdsConfigTabelaPesquisa.Locate('ORDEM_CAMPO_PESQUISA',(NumAtual + 1),[]) then
    begin
      cdsConfigTabelaPesquisa.Edit;
      cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value := NumAtual;
      cdsConfigTabelaPesquisa.Post;
      cdsConfigTabelaPesquisa.GotoBookmark(Book);
      cdsConfigTabelaPesquisa.Edit;
      cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value := NumAtual + 1;
      cdsConfigTabelaPesquisa.Post;
    end;
    {else
    begin
      cdsConfigTabelaPesquisa.Edit;
      cdsConfigTabelaPesquisaORDEM_CAMPO_PESQUISA.Value := NumAtual + 1;
      cdsConfigTabelaPesquisa.Post;
    end;}


  end;
end;

function TFrmConfiguracoes.VerificaArrayNumS(vArray: array of Integer;
  vNum: integer): Boolean;
var i , TamArray : Integer;
begin
  Result := False;
  TamArray := Length(vArray);
  for i := 0 to TamArray do
  begin
    if vArray[i] = vNum  then
    begin
      Result := True;
      Exit;
    end;

  end;
end;

end.
